package com.yihu.cms.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.fileManager.DirectoryTreeNode;
import com.yihu.beans.fileManager.Files;
import com.yihu.beans.fileManager.FmListConditions;
import com.yihu.beans.fileManager.Folder;
import com.yihu.cms.service.FileManagerService;
import com.yihu.dataService.common.DateUtil;

@Controller
public class FileManagerController extends BaseController {

	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	FileManagerService fs = new FileManagerService();
	
	@RequestMapping(value = "/filemanger/allDirectory", method = RequestMethod.GET)
	public @ResponseBody Object allDirectory()
	{
		log.info("filemanger get all directory .");
		List<DirectoryTreeNode> ldtn = fs.getAllDirectory();
		return getResult( 0 , ldtn );
	}

	@RequestMapping(value = "/filemanger/list", method = RequestMethod.GET)
	public @ResponseBody Object fileList(@RequestParam(value="path",required=false , defaultValue = "") String path,
			@RequestParam(value="order",required=false , defaultValue = "name") String order,
			@RequestParam(value="desc",required=false, defaultValue = "0") int desc,
			@RequestParam(value="type",required=false , defaultValue = "") String type, 
			@RequestParam(value="tag",required=false , defaultValue = "") String tag, 
			@RequestParam(value="startDate",required=false, defaultValue = "") String startDate, 
			@RequestParam(value="endDate",required=false, defaultValue = "") String endDate
			) {
		log.info("filemanger list .");
		FmListConditions fmlc = new FmListConditions();
		List<Files> files = new ArrayList<Files>();
		if(order!=null && !"".equals(order))
		{
			if("name".equals(order))
				fmlc.setOrder("fileName");
			else if("time".equals(order))
				fmlc.setOrder("createTime");
			else if("size".equals(order))
				fmlc.setOrder("fileSize");
			else
			{
				log.info("unknown order . order=" + order);
				return getResult(1 , files);
			}
		}
		if(startDate != null && startDate.trim().length()>0)
			fmlc.setStartDate(DateUtil.stringToDate(startDate));
		if(endDate != null && endDate.trim().length()>0)
			fmlc.setEndDate(DateUtil.stringToDate(endDate));
		
		if(StringUtils.isNotBlank(path)){
			fmlc.setParentFolder(path);
		}
		if(StringUtils.isNotBlank(tag)){
			fmlc.setTag(tag);
		}
		if(StringUtils.isNotBlank(type)){
			fmlc.setType(type);
		}
		files = fs.getList(fmlc);
		FMListreponse fmr = new FMListreponse();
		fmr.setFiles(files);
		fmr.setPath(path);
		fmr.setTotalCount(files.size());
		return getResult( 0 , fmr );
	}

	@RequestMapping(value = "/filemanger/delete", method = RequestMethod.POST)
	public @ResponseBody Object fileDel(@RequestBody Files[] files) {
		int result = fs.del(files);//path, 
		log.info("result=" + result);
		return getResult(result, null);
	}

	@RequestMapping(value = "/filemanger/create", method = RequestMethod.POST)
	public @ResponseBody Object fileCreate(@RequestBody Folder folder) {
		log.info("create filemanger folder=" + folder );
		Files file = new Files();
		if(StringUtils.isNotBlank(folder.getFileType())){
			file.setFileType(folder.getFileType());
		}else{
			file.setFileType("folder");
		}
		file.setFileName(folder.getFileName());
//		file.setDirectory(true);
		file.setCreateTime(new Date());
		file.setParentFolder(folder.getPath());
		file.setPicture(folder.getPicture());

		int result = fs.create( file );
		log.info("result=" + result);
		return getResult(result, null);

		
//		log.info("create filemanger folder=" + folder );
//		Files file = new Files();
//		file.setFileType("folder");
//		file.setFileName(folder.getFileName());
//		file.setDirectory(true);
//		file.setCreateTime(new Date());
//		file.setParentFolder(folder.getPath());
//
//		int result = fs.create( file );
//		log.info("result=" + result);
//		return getResult(result, null);
	}

	@RequestMapping(value = "/filemanger/rename", method = RequestMethod.POST)
	public @ResponseBody Object fileRename(@RequestBody Folder folder) {
//		log.info("create filemanger path=" + path + " , newfName=" + newfName);
		int result = fs.rename(folder);

		log.info("result=" + result);
		return getResult(result, null);
	}

	@RequestMapping(value = "/filemanger/move", method = RequestMethod.POST)
	public @ResponseBody Object fileMove(@RequestBody Files[] files,
			@RequestParam(value = "dest", required = false, defaultValue = "")String dest ) {
		log.info("create filemanger path=" + dest);
		int result = fs.move(files, dest);

		log.info("result=" + result);
		return getResult(result, null);
	}

	@RequestMapping(value = "/filemanger/copy", method = RequestMethod.POST)
	public @ResponseBody Object fileCopy(@RequestBody Files[] files,
			@RequestParam(value = "dest", required = false, defaultValue = "") String dest) {
		log.info("create filemanger path=" + dest);
		int result = fs.copy(files, dest);

		log.info("result=" + result);
		return getResult(result, null);
	}
	
	private class FMListreponse{
		List<Files> files = null;
		String path = "";
		int totalCount = 0;
		
		public List<Files> getFiles() {
			return files;
		}
		public void setFiles(List<Files> files) {
			this.files = files;
		}
		public String getPath() {
			return path;
		}
		public void setPath(String path) {
			this.path = path;
		}
		public int getTotalCount() {
			return totalCount;
		}
		public void setTotalCount(int totalCount) {
			this.totalCount = totalCount;
		}
	}

}
